package org.zachary.creditbusiness.commodityandhouse.controller;

import org.springframework.web.bind.annotation.*;
import org.zachary.creditbusiness.commodityandhouse.entity.Commodity;
import org.zachary.creditbusiness.commodityandhouse.service.CommodityService;
import org.zachary.creditbusiness.commodityandhouse.dto.CommodityWithPurchaseCount;

import java.util.List;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.zachary.creditbusiness.commodityandhouse.dto.CommodityWithPurchaseCount;
@RestController
@RequestMapping("/commodities")
public class CommodityController {

    @Autowired
    private CommodityService commodityService;

    @GetMapping("/{id}")
    public Commodity getCommodity(@PathVariable Integer id) {
        return commodityService.getCommodityById(id);
    }

    // 保留原有的不分页接口
    @GetMapping
    public List<Commodity> getAllCommodities() {
        return commodityService.getAllCommodities();
    }

    // 分页获取商品列表 - 主要使用这个
    @GetMapping("/list")
    public Page<Commodity> getCommodities(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        return commodityService.getCommodities(page, size);
    }

    // 按标题搜索 - 支持分页
    @GetMapping("/searchByName")
    public Page<Commodity> searchByName(
            @RequestParam String name,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        return commodityService.searchByName(name, page, size);
    }

    // 按分类筛选 - 支持分页
    @GetMapping("/filterByCategory")
    public Page<Commodity> filterByCategory(
            @RequestParam String category,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        return commodityService.filterByCategory(category, page, size);
    }

    // 按价格范围筛选 - 支持分页
    @GetMapping("/filterByPriceRange")
    public Page<Commodity> filterByPriceRange(
            @RequestParam(required = false) Double minPrice,
            @RequestParam(required = false) Double maxPrice,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        return commodityService.filterByPriceRange(minPrice, maxPrice, page, size);
    }

    // 综合筛选接口 - 支持多条件同时筛选
    @GetMapping("/filter")
    public Page<Commodity> filterCommodities(
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String category,
            @RequestParam(required = false) Double minPrice,
            @RequestParam(required = false) Double maxPrice,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        return commodityService.filterCommodities(name, category, minPrice, maxPrice, page, size);
    }


    // 根据用户ID获取购买最多的前三个商品
    @GetMapping("/top-purchased")
    public List<CommodityWithPurchaseCount> getTopThreePurchasedCommodities(@RequestParam("userId") Integer userId) {
        return commodityService.getTopThreePurchasedCommoditiesByUserId(userId);
    }


    @PostMapping
    public Commodity createCommodity(@RequestBody Commodity commodity) {
        return commodityService.createCommodity(commodity);
    }

    @PutMapping("/{id}")
    public Commodity updateCommodity(@PathVariable Integer id, @RequestBody Commodity commodity) {
        commodity.setId(id);
        return commodityService.updateCommodity(commodity);
    }

    @DeleteMapping("/{id}")
    public void deleteCommodity(@PathVariable Integer id) {
        commodityService.deleteCommodity(id);
    }













}
